package com.ifood.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ifood.common.core.domain.R;
import com.ifood.common.core.domain.entity.FoodProductCategory;
import com.ifood.common.core.domain.vo.ProductCategoryVo;
import com.ifood.system.service.IFoodProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ifood.common.annotation.Log;
import com.ifood.common.core.controller.BaseController;
import com.ifood.common.core.domain.AjaxResult;
import com.ifood.common.enums.BusinessType;
import com.ifood.common.utils.poi.ExcelUtil;
import com.ifood.common.core.page.TableDataInfo;

/**
 * 商品分类Controller
 *
 * @author ifood
 * @date 2024-01-14
 */
@RestController
@Api(tags = "商品分类")
@RequestMapping("/system/category")
public class FoodProductCategoryController extends BaseController {
    @Autowired
    private IFoodProductCategoryService foodProductCategoryService;

    /**
     * 查询商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:list')")
    @GetMapping("/list")
    @ApiOperation("查询商品分类列表")
    public TableDataInfo list(FoodProductCategory foodProductCategory) {
        startPage();
        List<FoodProductCategory> list = foodProductCategoryService.selectFoodProductCategoryList(foodProductCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:export')")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FoodProductCategory foodProductCategory) {
        List<FoodProductCategory> list = foodProductCategoryService.selectFoodProductCategoryList(foodProductCategory);
        ExcelUtil<FoodProductCategory> util = new ExcelUtil<FoodProductCategory>(FoodProductCategory.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:category:query')")
    @GetMapping(value = "/{categoryId}")
    @ApiOperation("获取商品分类详细信息")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return success(foodProductCategoryService.selectFoodProductCategoryByCategoryId(categoryId));
    }

    /**
     * 新增商品分类
     */
    @PreAuthorize("@ss.hasPermi('system:category:add')")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增商品分类")
    public AjaxResult add(@RequestBody FoodProductCategory foodProductCategory) {
        return toAjax(foodProductCategoryService.insertFoodProductCategory(foodProductCategory));
    }

    /**
     * 修改商品分类
     */
    @PreAuthorize("@ss.hasPermi('system:category:edit')")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改商品分类")
    public AjaxResult edit(@RequestBody FoodProductCategory foodProductCategory) {
        return toAjax(foodProductCategoryService.updateFoodProductCategory(foodProductCategory));
    }

    /**
     * 删除商品分类
     */
    @PreAuthorize("@ss.hasPermi('system:category:remove')")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    @ApiOperation("删除商品分类")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(foodProductCategoryService.deleteFoodProductCategoryByCategoryIds(categoryIds));
    }

    /**
     * 获取商品类目下拉列表
     */
    @GetMapping("productCategories")
    @ApiOperation("获取商品类目下拉列表")
    public R<List<ProductCategoryVo>> selectProductCategory() {
        return R.ok(foodProductCategoryService.selectProductCategories());
    }
}
